<?php

namespace App\Admin\Controllers;

use App\Models\Slideshow;

use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Layout\Content;
use App\Http\Controllers\Controller;
use Encore\Admin\Controllers\ModelForm;
use Illuminate\Support\Facades\Log;

class SlideshowController extends Controller
{
    use ModelForm;

    /**
     * Index interface.
     *
     * @return Content
     */
    public function index()
    {

        return Admin::content(function (Content $content) {
            $content->header('轮播图管理');
            $content->description('轮播图最多添加5张，如需更换请删除已弃用轮播图！');

            $content->body($this->grid());
        });
    }

    /**
     * Edit interface.
     *
     * @param $id
     * @return Content
     */
    public function edit($id)
    {
        return Admin::content(function (Content $content) use ($id) {

            $content->header('修改轮播图');

            $content->body($this->form()->edit($id));
        });
    }

    /**
     * Create interface.
     *
     * @return Content
     */
    public function create()
    {
        return Admin::content(function (Content $content) {

            $content->header('新增轮播图');

            $content->body($this->form());
        });
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        Admin::script("popverLoad()");
        $slideshows = Slideshow::where('deleted_at', null)
            ->select('id', 'sort')
            ->orderBy('sort', 'desc');

        $maximum = $slideshows->get()->toArray();
        $maximum = array_pluck($maximum, 'sort');

        $total = $slideshows->count();

        return Admin::grid(Slideshow::class, function (Grid $grid) use ($maximum, $total){

            if ($total >= 5)
                $grid->disableCreation();

            $grid->disableExport();
            $grid->model()->orderBy('sort', 'asc');
            $grid->paginate(5);
            $grid->disablePagination();
            $grid->disableFilter();

            $grid->id('ID')->sortable();
            $grid->title(trans('admin.title'));
            $grid->column('url', '轮播图')->display(function ($value) {
                $url = asset('uploads/' . $value);
                return '
                    <a class="layui-btn layui-btn-primary slideshows-popver" href="javascript:;" src-data="' . $url . '">预览</a>
                ';
            });
            $grid->column('link', '链接')->display(function ($value) {
                return '<a href="' . $value . '" target="_blank">查看</a>';
            });

            $grid->column('sort', '排序')->display(function ($value) use ($maximum){
                $html = '';

                if (min($maximum) == $value)
                {
                    $html .= '<a href="javascript:;" class="sortClass" attr-action="down" attr-content="'. $value .'" >下移</a>';
                } elseif (max($maximum) == $value) {
                    $html .= '<a href="javascript:;" class="sortClass" attr-action="up" attr-content="'. $value .'">上移</a>';
                } else {
                    $html .= '<a href="javascript:;" class="sortClass" attr-action="up" attr-content="'. $value .'">上移</a>&nbsp;';
                    $html .= '<a href="javascript:;" class="sortClass" attr-action="down" attr-content="'. $value .'" >下移</a>';
                }
                return $html;
            });

            $grid->created_at('上线时间');
//            $grid->updated_at('更新时间');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Admin::form(Slideshow::class, function (Form $form) {

            $form->text('title', trans('admin.title'))->rules('required');

            $form->text('summary', '简介')
                ->attribute('maxlength', '50');

            $form->url('link', '链接')
                ->help('示例：https://www.example.com/example, 默认为首页')
                ->default(route('index'))
                ->rules('url');

            $form->image('url', '轮播大图')
                ->help('建议尺寸 1920 * 778 , 图片大小小于5MB')
                ->uniqueName()
                ->removable()
                ->attribute('accept', 'image/png, image/jpeg, image/jpg')
                ->help('建议尺寸：1920 * 778, 不大于5Mb. 支持格式：png、jpeg、jpg')
                ->rules('required|image|max:5120');

            $slideshows = Slideshow::where('deleted_at', null)
                ->select('id', 'sort')
                ->orderBy('sort', 'asc')
                ->get();

            $maxSort = count($slideshows) >= 1 ? max($slideshows->toArray()) : 0;

            if ($maxSort >= 5)
            {
                $insetSort = [];
                foreach ($slideshows as $key => $item)
                {
                    $itemSort = $key + 1;
                    $item->sort = $itemSort;
                    $item->save();

                    $insetSort[] = $itemSort;
                }
            }

            $sort = isset($insetSort) ? max($insetSort) : $maxSort;
            $sort += 1;

            $form->hidden('sort')->value($sort);
//            $form->display('created_at', 'Created At');
//            $form->display('updated_at', 'Updated At');
        });
    }

}
